// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/connectors/hive/table_handle.h>

#include <gtest/gtest.h>
#include <pollux/testing/gtest_utils.h>
#include <pollux/testing/exec/util/hive_connector_test_base.h>

using namespace kumo::pollux;

TEST(FileHandleTest, hiveColumnHandle) {
  Type::registerSerDe();
  connector::hive::HiveColumnHandle::registerSerDe();
  auto columnType = ROW(
      {{"c0c0", BIGINT()},
       {"c0c1",
        ARRAY(MAP(
            VARCHAR(), ROW({{"c0c1c0", BIGINT()}, {"c0c1c1", BIGINT()}})))}});
  auto columnHandle = exec::test::HiveConnectorTestBase::makeColumnHandle(
      "columnHandle", columnType, columnType, {"c0.c0c1[3][\"foo\"].c0c1c0"});
  ASSERT_EQ(columnHandle->name(), "columnHandle");
  ASSERT_EQ(
      columnHandle->columnType(),
      connector::hive::HiveColumnHandle::ColumnType::kRegular);
  ASSERT_EQ(columnHandle->dataType(), columnType);
  ASSERT_EQ(columnHandle->hiveType(), columnType);
  ASSERT_FALSE(columnHandle->isPartitionKey());

  auto str = columnHandle->toString();
  auto obj = columnHandle->serialize();
  auto clone =
      ISerializable::deserialize<connector::hive::HiveColumnHandle>(obj);
  ASSERT_EQ(clone->toString(), str);

  auto incompatibleHiveType = ROW({{"c0c0", BIGINT()}, {"c0c1", BIGINT()}});
  POLLUX_ASSERT_THROW(
      exec::test::HiveConnectorTestBase::makeColumnHandle(
          "columnHandle",
          columnType,
          incompatibleHiveType,
          {"c0.c0c1[3][\"foo\"].c0c1c0"}),
      "data type ROW<c0c0:BIGINT,c0c1:ARRAY<MAP<VARCHAR,ROW<c0c1c0:BIGINT,c0c1c1:BIGINT>>>> and hive type ROW<c0c0:BIGINT,c0c1:BIGINT> do not match");
}
